package com.oceantrader.sale.biz.hello.dao;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.oceantrader.sale.biz.hello.entity.Hello;
import com.oceantrader.root.tools.Dao;
import com.oceantrader.root.tools.Dao2;
import com.oceantrader.root.tools.Sql;
import com.oceantrader.root.tools.SqlLoader;
import com.oceantrader.root.tools.Dao.Transaction;
import com.oceantrader.root.tools.SqlLoader.SQL;

import io.vertx.core.Future;
import lombok.RequiredArgsConstructor;

@Component
@RequiredArgsConstructor
public class HelloDao {
    private final Dao dao;
    private final Dao2 dao2;

    public Future<List<Hello>> getHelloList(Future<Transaction> tx, Hello param) {
        Sql sql = new Sql();
        sql.append("SELECT * FROM `hello`");
        sql.append("WHERE").ignore("AND");
        if (param.getId() != null) {
            sql.append("AND `id`=#{}", param.getId());
        }
        if (param.getName() != null) {
            sql.append("AND `name`=#{}", param.getName());
        }
        return dao.selectList(tx, sql, Hello.class);
    }

    public Future<List<Hello>> getHelloList2(Future<Dao2.Transaction> tx, Hello param) {
        String xsql = """
                SELECT * FROM `hello`
                WHERE [?AND]
                if(param.getId()!= null){
                    AND `id`=[#param.getId()]
                }
                if(param.getName()!=null){
                    AND `name`=[#param.getName()]
                }
                [?WHERE]
                """;
        SQL sql = SqlLoader.load(xsql, Map.of("param", param));
        return dao2.selectList(null, sql, Hello.class);
    }

    public Future<Long> addHello(Future<Transaction> tx, Hello param) {
        Sql sql = new Sql();
        sql.append("INSERT INTO `hello`(");
        if (param.getName() != null) {
            sql.append("`name`,");
        }
        if (param.getContent() != null) {
            sql.append("`content`,");
        }
        sql.ignore(",").append(") VALUES (");
        if (param.getName() != null) {
            sql.append("#{},", param.getName());
        }
        if (param.getContent() != null) {
            sql.append("#{},", param.getContent());
        }
        sql.ignore(",").append(")");
        return dao.insert(tx, sql);
    }

    public Future<Integer> delHello(Future<Transaction> tx, List<Long> idList) {
        Sql sql = new Sql();
        sql.append("DELETE FROM `hello`");
        sql.append("WHERE").ignore("AND");
        sql.append("`id` IN").forEachParams("(", idList, ",", ")");
        return dao.update(tx, sql);
    }

    public Future<Integer> updateById(Future<Transaction> tx, Hello param) {
        Sql sql = new Sql();
        sql.append("UPDATE `hello`");
        sql.append("SET").ignore(",");
        if (param.getName() != null) {
            sql.append(",`name`=#{}", param.getName());
        }
        if (param.getContent() != null) {
            sql.append(",`content`=#{}", param.getContent());
        }
        sql.append("WHERE `id`=#{}", param.getId());
        return dao.update(tx, sql);
    }

    public Future<Integer> updateNullById(Future<Transaction> tx, Hello param) {
        Sql sql = new Sql();
        sql.append("UPDATE `hello`");
        sql.append("SET").ignore(",");
        sql.append(",`name`=#{}", param.getName());
        sql.append(",`content`=#{}", param.getContent());
        sql.append("WHERE `id`=#{}", param.getId());
        return dao.update(tx, sql);
    }
}
